const https = require('https');
var http = require('http')
var url = require('url')

var server = http.createServer()


server.on('request',function(req,res){
    var urlObj = url.parse(req.url)
    var pathname = urlObj.pathname
    if(pathname === '/send'){
        send(function(data){
            res.end('1232' + data)
        },function(){
            console.log('fail')
            res.end('fail')
        })
    }
})

server.listen(4000,function(){
    console.log('server start at port 4000')
})

function send(success,fail){
    const options = {
        hostname: 'www.baidu.com',
        port: 443,
        path: '/',
        method: 'GET'
    };
    const request = https.request(options, (response) => {
        response.on('data', (chunk) => {
            response.setEncoding('utf8');
            console.log(chunk)
            success && success(chunk)
        });
    });
    request.on('error', (e) => {
        fail && fail()
    });
    request.end();
}
